$(function () {
    // 用户地址数据渲染
    getAddress();
    function getAddress() {
        $.ajax({
            method: 'get',
            url: '/addressBook/default',
            success: function (res) {
                console.log(res);
                if (res.code === 0) {
                    $('.address').html('黑马程序员4楼');
                    $('#xm').html('李四');
                    $('#sex').html('先生');
                    $('#telp').html('18868816185');
                    $('#sj').html('17:51');
                } else {
                    let sex = null;
                    if (res.data.sex === 0) {
                        sex = '女士';
                    } else {
                        sex = '先生';
                    }
                    $('.address').html(res.data.detail);
                    $('#xm').html(res.data.consignee);
                    $('#sex').html(sex);
                    $('#telp').html(res.data.phone);
                    $('.spend').attr('data-id', res.data.id);

                    // 自定义订单送达时间
                    const dt = new Date();
                    let h = dt.getHours();
                    let m = dt.getMinutes();
                    h = h + 1;
                    h = h < 10 ? '0' + h : h;
                    m = m < 10 ? '0' + m : m;
                    const sj = `${h}:${m}`;
                    $('#sj').html(sj);
                }
            }
        });
    }

    // 新增订单数据渲染
    getOrder();
    function getOrder() {
        $.ajax({
            method: 'get',
            url: '/shoppingCart/list',
            success: function (res) {
                console.log(res);
                const htmlStr = template('tpl-order', res);
                $('.itemList').html(htmlStr);
                if (res.data.length === 0) {
                    $('.divGoodsNum').css('display', 'none');
                    $('.sum-price').html('0');
                    $('.divCart').css('background', '#d8d8d8');
                    $('.person-car').removeClass('imgCartActive').addClass('imgCart');
                    $('.spend').removeClass('btnSubmitActive').addClass('btnSubmit').off('click');
                } else {
                    // 订单总数
                    let arr = res.data.map(value => {
                        return parseInt(value.number);
                    });
                    let count = 0;
                    arr.forEach(value => {
                        count += value;
                        $('.divGoodsNum').html(count);
                    });

                    // 订单总价
                    let arr1 = res.data.map(value => {
                        return (value.number) * (value.amount);
                    });
                    let money = 0;
                    arr1.forEach(value => {
                        money += value;
                        $('.sum-price').html((money).toFixed(2));
                    });
                }
            }
        });
    }

    // 去支付
    $('.spend').on('click', function () {
        $.ajax({
            method: 'post',
            url: '/order/submit',
            data: JSON.stringify({
                addressBookId: $(this).attr('data-id'),
                payMethod: 1,
                remark: ""
            }),
            contentType: "application/json",
            success: function (res) {
                console.log(res);
            }
        });
    });

    // 备注区域效果
    $('textarea').on('input', function () {
        let num = $(this).val().trim().length;
        $('.van-field__word-num').html(num);
    });

    // 前往上或下一个页面事件
    $('.divTitle i').on('click', function () {
        location.href = '/index.html';
    });
    $('.divAddress i').on('click', function () {
        location.href = '/address.html';
    });
})
